﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace TeachingAffairsManagementSystem
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        void Session_Start(object sender, EventArgs e)
        {
            Session["isLoggedIn"] = false;
            // 获取客户端cookies
            HttpCookie loginDataCookie = Request.Cookies.Get("login_data");
            if (loginDataCookie != null)
            {
                string username = loginDataCookie.Values["username"];
                string password = loginDataCookie.Values["password"];
                DataSet loginDataSet = Operation.Login(username, password);
                if (loginDataSet.Tables[0].Rows.Count > 0)
                {
                    // 登录成功
                    DataRow loginDataRow = loginDataSet.Tables[0].Rows[0];
                    string userType = (string) loginDataRow["user_type"];
                    int id = Convert.ToInt32(loginDataRow["id"]);
                    string name = (string) loginDataRow["name"];

                    // 设置session 保持登录状态
                    Session["isLoggedIn"] = true;
                    Session["userType"] = userType;
                    Session["id"] = id;
                    Session["name"] = name;
                }
            }
        }
    }
}